package cn.t.core.util;

import cn.t.core.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

@Slf4j
public class I18nUtils {

    public static String getMessage(String key) {
        Locale locale = LocaleContextHolder.getLocale();
        if (Locale.CHINESE.equals(locale)) {
            locale = Locale.SIMPLIFIED_CHINESE;
        }
        MessageSource messageSource = SpringContextUtil.getBean(MessageSource.class);
        return messageSource.getMessage(key, null, locale);
    }

    /**
     *  获取资源文件中的信息 有format需求
     */
    public static String getMessage(String key, Object[] messages) {
        Locale locale = LocaleContextHolder.getLocale();
        if (Locale.CHINESE.equals(locale)) {
            locale = Locale.SIMPLIFIED_CHINESE;
        }
        MessageSource messageSource = SpringContextUtil.getBean(MessageSource.class);
        if(messages == null || messages.length == 0) {
            return messageSource.getMessage(key, null, locale);
        }

        return  messageSource.getMessage(key, messages, locale);
    }

}
